package cn.njyd.corejava.thread.myday03;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class TrafficSignal{
    private int flag = 1;//1红灯，2绿，3黄
    private Lock lock = new ReentrantLock();

    private Condition con_red = lock.newCondition();

    private Condition con_green = lock.newCondition();

    private Condition con_yellow = lock.newCondition();

    public void redLight(){
        lock.lock();
        try{
            while(flag!=1) {
                con_red.await();
            }
            System.out.println("红灯");
            for (int i=1;i<=5;i++) {
                System.out.println("-"+i);
                Thread.sleep(1000);
            }
            flag = 2;
            con_green.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void greenLight(){
        lock.lock();
        try{
            while(flag!=2) {
                con_green.await();
            }
            System.out.println("绿灯");
            for (int i=1;i<=5;i++) {
                System.out.println("-"+i);
                Thread.sleep(1000);
            }
            flag = 3;
            con_yellow.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void yellowLight(){
        lock.lock();
        try{
            while(flag!=3) {
                con_yellow.await();
            }
            System.out.println("黄灯");
            for (int i=1;i<=5;i++) {
                System.out.println("-"+i);
                Thread.sleep(1000);
            }
            flag = 1;
            con_red.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

class RedLightThread implements Runnable{

    private TrafficSignal trafficSignal;

    public RedLightThread(TrafficSignal trafficSignal) {
        this.trafficSignal = trafficSignal;
    }

    @Override
    public void run() {
        while(true) {
            trafficSignal.redLight();
        }
    }
}

class YellowLightThread implements Runnable{

    private TrafficSignal trafficSignal;

    public YellowLightThread(TrafficSignal trafficSignal) {
        this.trafficSignal = trafficSignal;
    }

    @Override
    public void run() {
        while(true) {
            trafficSignal.yellowLight();
        }
    }
}

class GreenLightThread implements Runnable{

    private TrafficSignal trafficSignal;

    public GreenLightThread(TrafficSignal trafficSignal) {
        this.trafficSignal = trafficSignal;
    }

    @Override
    public void run() {
        while(true) {
            trafficSignal.greenLight();
        }
    }
}




public class ThreadAwaitSignalDemo02 {
    public static void main(String[] args) {
        TrafficSignal trafficSignal = new TrafficSignal();
        new Thread(new GreenLightThread(trafficSignal)).start();
        new Thread(new YellowLightThread(trafficSignal)).start();
        new Thread(new RedLightThread(trafficSignal)).start();


    }



}
